package com.tech.tool.service.impl;

import com.tech.tool.dataobject.ApiReview;
import com.tech.tool.enums.DeleteEnum;
import com.tech.tool.enums.ResultEnum;
import com.tech.tool.exception.ToolException;
import com.tech.tool.repository.ApiReviewRepository;
import com.tech.tool.service.ApiReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class ApiReviewServiceImpl implements ApiReviewService {

    @Autowired
    private ApiReviewRepository repository;

    @Override
    public ApiReview findOne(Integer apiId) {
//        ApiReview apiReview = repository.findById(apiId).get();
//        ApiReview apiReview = repository.getOne(apiId);
//        if (apiReview == null) {
        throw new ToolException(ResultEnum.APIREVIEW_NOT_EXSIT);
//        throw new ToolException(ResultEnum.APIREVIEW_NOT_EXSIT);
//        }
//        return apiReview;
    }

    @Override
    public Page<ApiReview> findByProjectId(Integer projectId, Pageable pageable ) {
        Page<ApiReview> apiReviewPage = repository.findByApiDeleteAndProjectId(
                DeleteEnum.EXIST.getCode(), projectId, pageable);
        return apiReviewPage;
    }

    @Override
    public ApiReview save(ApiReview apiReview) {
        return repository.save(apiReview);
    }

    @Override
    public ApiReview updateStatus(Integer apiReviewId, Integer status) {
        ApiReview apiReview = findOne(apiReviewId);
        apiReview.setApiStatus(status);
        return repository.save(apiReview);
    }

    @Override
    public Page<ApiReview> findByProjectIdAndUserIdAndApiName(Integer projectId, Integer userId, String apiName, Pageable pageable) {
        Page<ApiReview> apiReviewPage = repository.findOptionalByApiDeleteAndProjectIdAndUserIdAndApiName(
                DeleteEnum.EXIST.getCode(), projectId, userId, apiName, pageable);
        return apiReviewPage;
    }

    @Override
    public ApiReview delete(Integer apiReviewId, Integer delete) {
        ApiReview apiReview = findOne(apiReviewId);
        apiReview.setApiDelete(delete);
        return repository.save(apiReview);
    }
}
